﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：FormsContextMenu.cs
           // 文件功能描述：窗体的右键菜单。
           //
           // 
           // 创建标识：杨波20081007
           //
           // 修改标识：
           // 修改描述：
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System.IO;
using System.Text;
using RuleCode.compiler;

#endregion

namespace RuleCode.ObjectTree
{
    public partial class FormsContextMenu : NewAndCompileContext
    {
        private string name;

        public FormsContextMenu()
        {
            InitializeComponent();
        }

        protected override void addNewNode()
        {
            base.addNewNode();
            name = Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.Forms, OT.Form, typeof(FormProperty), "");
            //Util.SelectedNode = (SysTreeNode)Util.SelectedNode.Nodes.Find("Designs", true).First();
            foreach (SysTreeNode treeNode in Util.SelectedNode.Nodes)
            {
                if (treeNode.Text.Equals("Designs"))
                {
                    Util.SelectedNode = treeNode;
                }
            }
            Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.FormDesigns, OT.Panel,
                         typeof(PanelProperty), "");
            //CreateFile();
        }

        private void CreateFile()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("#region using");
            sb.AppendLine("using System;");
            sb.AppendLine("using RuleCode.ObjectTree;");
            sb.AppendLine("using RuleCode.Table;");
            sb.AppendLine("using RuleCode.Quries;");
            sb.AppendLine("using RuleCode.Common;");
            sb.AppendLine("using RuleCode.Enum;");
            sb.AppendLine("using RuleCode.ExtendType;");
            //sb.AppendLine("using RuleCode.CurrentUser;");
            ////sb.AppendLine("using RuleCode.FrameWork;");
            //sb.AppendLine("using RuleCode.ObjectTree;");
            //sb.AppendLine("using RuleCode.Query;");
            //sb.AppendLine("using RuleCode.ObjectTree.Enum;");
            //sb.AppendLine("using RuleCode.ObjectTree.ExtendDataType;");
            //sb.AppendLine("using RuleCode.ObjectTree.Table;");
            //sb.AppendLine("using RuleCode.Query.AOTCommonClass;");
            //sb.AppendLine("using RuleCode.Query.Enum;");
            ////sb.AppendLine("using RuleCode.Query.Forms;");
            //sb.AppendLine("using RuleCode.Query.InheritControl;");
            ////sb.AppendLine("using RuleCode.Query.Language;");
            //sb.AppendLine("using RuleCode.Query.Queries;");
            //sb.AppendLine("using RuleCode.Query.SQL;");
            //sb.AppendLine("using RuleCode.Query.Tables;");
            //sb.AppendLine("using RuleCode.Query.Type;");
            sb.AppendLine("#endregion");

            sb.AppendLine("namespace " + SysDefalutNamespace.DefaultFormNamespace);
            sb.AppendLine("{");
            sb.AppendLine("\tpublic partial class " + name);
            sb.AppendLine("\t{");
            sb.AppendLine("\t}");
            sb.AppendLine("}");
            var dir = new DirectoryInfo(Util.GetFormsPath );//(Util.OTPath + @"\Module\Forms");
            if (!dir.Exists)
            {
                dir.Create();
            }
            StreamWriter swr =
                new StreamWriter(Util.GetFormsPath + "\\" + Util.SelectedNode.Property.ID + ".cs", false,
                                 Encoding.UTF8);

            swr.Write(sb);

            swr.Flush();

            swr.Close();
        }

        protected override void CompileNode()
        {
            base.CompileNode();
            SysCompiler.Instance.compileForms(Util.SelectedNode);
        }
    }
}